#include"Socket.hpp"
#include"pro.hpp"
//写客户端
void Usager(const std::string& proc)
{
    std::cout<<"\nUsage"<<proc<<"serverip serverport"<<std::endl;

}
int main(int argc,char* argv[])
{
    if(argc!=3)
    {
        Usager(argv[0]);
        exit(0);
    }
    std::string serverip=argv[1];
    std::uint16_t serverport=std::stoi(argv[2]);

    Sock sockfd;
    sockfd.Socket();
    bool r=sockfd.Connect(serverip,serverport);
    if(!r)
        return 1;
    
    srand(time(nullptr)^getpid());
    std::string opers="+-*/%";
    std::string inbuffer_stream;

    int cnt=1;
    while(cnt<10)
    {
        std::cout<<"=====第"<<cnt<<"次测试-----------"<<std::endl;
        int x=rand()%100+1;
        usleep(1);
        int y=rand()%100+1;
        usleep(1);
        char op=opers[rand()%opers.size()];
        Request req(x,y,op);
        
        std::string package;
        req.Serialize(&package);

        char buffer[128];
        ssize_t n=read(sockfd.Fd(),buffer,sizeof(buffer));
        if(n>0)
        {
            buffer[n]=0;
            inbuffer_stream+=buffer;
            std::cout<<inbuffer_stream<<std::endl;
            std::string content;
            bool r=Decode(package,&content);

            Response resp;
            r=resp.Deserialize(content);
            resp.DubugPrint();
        }
        std::cout<<"========================"<<std::endl;
        cnt++;
    }

}